package org.fastsyncer.rest.ws;

import javax.jws.WebService;

import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;
import org.fastsyncer.common.entity.RestResult;
import org.fastsyncer.common.util.JsonUtil;
import org.fastsyncer.core.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 对外暴露WebService服务，提供外部主动推送数据给fastsyncer,实现数据同步.
 * @ClassName: WSImpl 
 * @Description: 接收第三方应用数据，包括新增、修改、删除事件的数据.
 * @author AE86
 * @date: 2017年7月19日 下午5:11:05
 */
@WebService(serviceName = "WSImpl", endpointInterface = "org.fastsyncer.rest.ws.WS")
public class WSImpl implements WS {

    @Autowired
    private ManagerService manager;
    
	@Override
	public String handle(String msg) {
		try {
			if(StringUtils.isBlank(msg)){
				throw new NullArgumentException("同步消息不能为空!");
			}
			
			// 处理同步消息
			manager.sync(msg);
		} catch (Exception e) {
			RestResult restFail = RestResult.restFail(e.getLocalizedMessage());
			return JsonUtil.objToJson(restFail);
		}
		return JsonUtil.objToJson(RestResult.restSuccess("推送成功!"));
	}

}